function insertArray (arr, val, compare, maxLen) {
  const index = arr.findIndex(compare)
  // arr已存在val
  if (index > -1) return
  // 删除arr最后一个元素
  if (maxLen && arr.length >= maxLen) {
    arr.pop()
  }
  arr.unshift(val)
}
function deleteForm (arr, compare) {
  const index = arr.findIndex(compare)
  if (index > -1) {
    arr.splice(index, 1)
  }
}
/**
 * 添加喜欢的曲目
 * @param item 切换的歌曲
 * @param key
 * @param compare 函数
 */
export function save (item, key, compare) {
  const items = JSON.parse(sessionStorage.getItem(key)) || []
  insertArray(items, item, compare)
  sessionStorage.setItem(key, JSON.stringify(items))
  return items
}
export function remove (key, compare) {
  const items = JSON.parse(sessionStorage.getItem(key)) || []
  deleteForm(items, compare)
  sessionStorage.setItem(key, JSON.stringify(items))
  return items
}
export function load (key) {
  return JSON.parse(sessionStorage.getItem(key))
}
